package com.gopay.webtest.controller;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.webtest.controller.base.BaseController;
import com.gopay.webtest.handler.Trans5555SignHandler;
import com.gopay.webtest.util.DateUtils;
import com.gopay.webtest.vo.AuthBankCardVO;
import com.gopay.webtest.xml.XmlDao;

/**
 * 网关银行卡鉴权Controller
 * 
 * AuthController.java
 * @author qianld
 * @2015-05-13  www.gopay.com.cn Inc.All rights reserved.
 */
@RequestMapping("/auth")
@Controller
public class AuthController extends BaseController {

    @Resource(name = "xmlDao")
    private XmlDao xmlDao;

    @Resource(name = "trans5555SignHandler")
    private Trans5555SignHandler trans5555SignHandler;

    /**
     * 跳转到申请页面
     * 
     * @return
     */
    @RequestMapping("/authentication")
    public ModelAndView apply() {
        ModelAndView mv = new ModelAndView();

        try {
        	//
            Map<String, String> map = xmlDao.readData4Map("auth_apply", "apply");

            if (map.containsKey("gopayServerTime")) {
                map.remove("gopayServerTime");
                map.put("gopayServerTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }
            if (map.containsKey("tranDateTime")) {
                map.remove("tranDateTime");
                map.put("tranDateTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }

            mv = new ModelAndView("auth.apply");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到鉴权页面", e);
        }

        return mv;
    }

    /**
     * 执行申请
     * 
     * @return
     */
    @RequestMapping("/doAuth")
    public ModelAndView doApply(@ModelAttribute AuthBankCardVO applyVo) {
        ModelAndView mv = new ModelAndView();

        try {

            // 签名
        	trans5555SignHandler.sign(applyVo.getTranCode()+"_"+applyVo.getVersion(), "signType", "signValue", applyVo, getRequest());

            mv.setViewName("auth.doApply");
            mv.addObject("applyVo", applyVo);
        } catch (Exception e) {
            logger.error("执行申请", e);
        }

        return mv;
    }

}
